const JWT = think.service('jwt', 'common')
module.exports = class extends think.Controller {
  constructor (...args) {
    super(...args)
    this._before = []
  }
  async __before () {
    if (this._before.indexOf('jwt') > -1) {
      await this._jwt()
      if (!this.member) {
        return this.fail(1000, 'deny')
      }
    }
  }
  getIp () {
    return this.ctx.ip.indexOf('::ffff:') > -1 ? this.ctx.ip.replace('::ffff:', '') : this.ctx.ip
  }
  async _jwt () {
    const {token} = this.ctx.headers
    this.member = false
    if (token) {
      // _id openId nickName unionid sessionKey
      this.member = await JWT.decrypt(token)
    }
    return this.member
  }
}
